package com.fy.oa.zc.service;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fy.oa.zc.domain.FySummaryView;
import com.fy.oa.zc.exception.FyOAException;
import com.fy.oa.zc.manager.FySummaryViewManager;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.apps.collaboration.event.AbstractCollaborationEvent;
import com.seeyon.apps.collaboration.event.CollaborationCancelEvent;
import com.seeyon.apps.collaboration.event.CollaborationFinishEvent;
import com.seeyon.apps.collaboration.event.CollaborationProcessEvent;
import com.seeyon.apps.collaboration.event.CollaborationStartEvent;
import com.seeyon.apps.collaboration.event.CollaborationStepBackEvent;
import com.seeyon.apps.collaboration.event.CollaborationStopEvent;

public  class FyOAFormEventDealImpl implements FyOAFormEventDeal {

	private static final Log logger = LogFactory.getLog(FyOAFormEventDealImpl.class);

	private FySummaryViewManager fySummaryViewManager;

	private List<FyOAFormHandlerAccess> fyOAFormHandlerAccessList;

	public String deal(AbstractCollaborationEvent event) {
		try {
			FyOAFormHandlerAccess fyOAFormHandlerAccess = checkAccess(event.getTemplateCode(),
					event.getClass().getSimpleName());
			if (fyOAFormHandlerAccess != null) {
				FySummaryView view = fySummaryViewManager.getFySummaryView(event.getSummaryId());
				if (view != null) {
					if (event instanceof CollaborationProcessEvent) {
						return dealProcessEvent(fyOAFormHandlerAccess, view,
								((CollaborationProcessEvent) event).getAffair().getId());
					} else if (event instanceof CollaborationFinishEvent) {
						return dealProcessEvent(fyOAFormHandlerAccess, view,
								((CollaborationFinishEvent) event).getAffairId());
					} else if (event instanceof CollaborationStartEvent) {
						return dealStartEvent(fyOAFormHandlerAccess, view);
					} else if (event instanceof CollaborationStopEvent) {
						return dealStopEvent(fyOAFormHandlerAccess, view);
					} else if (event instanceof CollaborationCancelEvent) {
						return dealCancelEvent(fyOAFormHandlerAccess, view,
								((CollaborationCancelEvent) event).getUserId(),
								((CollaborationCancelEvent) event).getMessage());
					} else if (event instanceof CollaborationStepBackEvent) {
						return dealStepBackEvent(fyOAFormHandlerAccess, view);
					} else {
						pringLog(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + event.getClass().getSimpleName());
					}
				}
			}
			return null;
		} catch (BusinessException e) {
			throw new FyOAException("获取流程编号失败", e);
		} catch (Exception e) {
			throw new FyOAException("监听" + event.getClass().getSimpleName() + "事件发生异常", e);
		}
	}

	/**
	 * 设置流程简略视图管理
	 * 
	 * @param fySummaryViewManager
	 *            {@link FySummaryViewManager} 流程简略视图管理
	 */
	public void setFySummaryViewManager(FySummaryViewManager fySummaryViewManager) {
		this.fySummaryViewManager = fySummaryViewManager;
	}

	@Override
	public String dealProcessEvent(FyOAFormHandlerAccess fyOAFormHandlerAccess, FySummaryView view, long affairid) {
		if (view.getState() != 3) {
			return fyOAFormHandlerAccess.getFyOA_FormEventManager().dealProcessEvent(view, affairid);
		} else {
			if (!fySummaryViewManager.isInform(affairid))
				return fyOAFormHandlerAccess.getFyOA_FormEventManager().dealFinishEvent(view, affairid);
		}
		return null;
	}

	@Override
	public String dealFinishEvent(FyOAFormHandlerAccess fyOAFormHandlerAccess, FySummaryView view, long affairid) {
		return fyOAFormHandlerAccess.getFyOA_FormEventManager().dealFinishEvent(view, -1L);
	}

	@Override
	public String dealStartEvent(FyOAFormHandlerAccess fyOAFormHandlerAccess, FySummaryView view) {
		return fyOAFormHandlerAccess.getFyOA_FormEventManager().dealStartEvent(view);
	}

	@Override
	public String dealStopEvent(FyOAFormHandlerAccess fyOAFormHandlerAccess, FySummaryView view) {
		return fyOAFormHandlerAccess.getFyOA_FormEventManager().dealStopEvent(view);
	}

	@Override
	public String dealCancelEvent(FyOAFormHandlerAccess fyOAFormHandlerAccess, FySummaryView view, long userID,
			String msg) {
		return fyOAFormHandlerAccess.getFyOA_FormEventManager().dealCancelEvent(view, userID, msg);
	}

	@Override
	public String dealStepBackEvent(FyOAFormHandlerAccess fyOAFormHandlerAccess, FySummaryView view) {
		return fyOAFormHandlerAccess.getFyOA_FormEventManager().dealStepBackEvent(view);
	}

	public void setFyOAFormHandlerAccessList(List<FyOAFormHandlerAccess> fyOAFormHandlerAccessList) {
		this.fyOAFormHandlerAccessList = fyOAFormHandlerAccessList;
	}

	/**
	 * 校验权限，是否判断是否符合
	 * 
	 * @param code
	 * @param event
	 * @return
	 */
	private FyOAFormHandlerAccess checkAccess(String code, String eventName) {
		if (code != null && !"".equals(code)) {
			FyOAFormHandlerAccess fyOAFormHandlerAccess = null;
			for (int i = 0, size = fyOAFormHandlerAccessList.size(); i < size; i++) {
				fyOAFormHandlerAccess = fyOAFormHandlerAccessList.get(i);
				if (fyOAFormHandlerAccess.checkTemplateCode(code) && fyOAFormHandlerAccess.checkEvent(eventName)) {
					return fyOAFormHandlerAccess;
				}
				fyOAFormHandlerAccess = null;
			}
		}
		return null;
	}

	/**
	 * 打印日志
	 *
	 * @param msg
	 *            {@link Object} 日志内容
	 */
	private void pringLog(Object msg) {
		logger.info(msg);
		System.out.println(msg);
	}

}
